package com.yuhanginfo.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Announcement;
import com.yuhanginfo.modules.sys.entity.AnnouncementGroup;
import com.yuhanginfo.modules.sys.service.AnnouncementGroupService;
import com.yuhanginfo.modules.sys.service.AnnouncementService;

/**
 * 公告信息Controller
 * @author Y.j
 * @version 2017-09-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/system/announcement")
public class AnnouncementController extends BaseController {

	@Autowired
	private AnnouncementService announcementService;
	@Autowired
	private AnnouncementGroupService announcementGroupService;
	
	private static final  String REDIRECT = "redirect:";
	
	private static final  String LINK = "/sys/system/announcement/?repage";
	
	@ModelAttribute
	public Announcement get(@RequestParam(required=false) String id) {
		Announcement entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = announcementService.get(id);
		}
		if (entity == null){
			entity = new Announcement();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:announcement:view")
	@RequestMapping(value = {"list", ""})
	public String list(Announcement announcement, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Announcement> page = announcementService.findPage(new Page<Announcement>(request, response), announcement); 
		model.addAttribute("page", page);
		return "modules/sys/system/announcementList";
	}

	@RequiresPermissions("sys:announcement:view")
	@RequestMapping(value = "form")
	public String form(Announcement announcement, Model model) {
		List<AnnouncementGroup> agList=announcementGroupService.findAll();
		model.addAttribute("agList",agList);
		model.addAttribute("announcement", announcement);
		return "modules/sys/system/announcementForm";
	}

	@RequiresPermissions("sys:announcement:edit")
	@RequestMapping(value = "save")
	public String save(Announcement announcement, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, announcement)){
			return form(announcement, model);
		}
		announcementService.save(announcement);
		addMessage(redirectAttributes, "保存公告信息成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:announcement:edit")
	@RequestMapping(value = "delete")
	public String delete(Announcement announcement, RedirectAttributes redirectAttributes) {
		announcementService.delete(announcement);
		addMessage(redirectAttributes, "删除公告信息成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:announcement:edit")
	@RequestMapping(value = "updateStatus")
	public String updateStatus(Announcement announcement,  Model model) {
		announcementService.updateStatus(announcement);
		return REDIRECT+Global.getAdminPath()+LINK;
	}

}